package mf.com.Service.impl.sys;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import mf.com.Service.sys.SysRoleService;
import mf.com.dto.system.SysRoleDto;
import mf.com.entity.system.SysRole;
import mf.com.mapper.sys.SysRoleMapper;
import mf.com.mapper.sys.SysRoleUserMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lmf
 * @Create: 2024/3/27 20:40
 * Description:  角色服务层实现类
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Resource
    private SysRoleMapper sysRoleMapper;

    @Resource
    private SysRoleUserMapper roleUserMapper;

    @Override
    public SysRole saveSysRole(SysRole sysRole) {
        return sysRoleMapper.save(sysRole);
    }

    @Override
    public SysRole updateSysRole(SysRole sysRole) {
        return sysRoleMapper.update(sysRole);
    }

    @Override
    public Integer deleteSysRole(Integer id) {
       return sysRoleMapper.delete(id);
    }

    /**
     * 查询所有角色以及用户对应拥有的角色
     */

    @Override
    public Map<String,Object> selectAll(Integer userId) {
        // 查询所有角色
        final List<SysRole> list = sysRoleMapper.selectAll();
        final HashMap<String, Object> map = new HashMap<>();
        final Integer userIds = roleUserMapper.findByUserId(userId);
        map.put("allRolesList",list);
        map.put("sysUserRoles",userIds);

        return map;
    }


    /**
     * 角色分页查询
     */
    @Override
    public PageInfo<SysRole> findByPage(SysRoleDto sysRoleDto, Integer current, Integer limit) {
        PageHelper.startPage(current, limit);
        final List<SysRole> list = sysRoleMapper.findByPage(sysRoleDto);
        return new PageInfo<>(list);
    }
}
